import axios from 'axios'

const movie = {
    state: {
        hotMovies: {},
        newMovies: {},
        topMovies: {},
        movieTags: {
            title: '发现好电影',
            subjects: [
                {
                    title: '同时入选IMDB250和豆瓣电影250的电影',
                    href: 'https://m.douban.com/doulist/968362/',
                    color: '#FFAC2D'
                },
                {
                    title: '带你进入不正常的世界',
                    href: 'https://m.douban.com/doulist/16002',
                    color: '#FF4055'
                },
                {
                    title: '用电【影】来祭奠逝去的岁月',
                    href: 'https://m.douban.com/doulist/190343',
                    color: '#4F9DED'
                },
                {
                    title: '女孩们的故事【电影】',
                    href: 'https://m.douban.com/doulist/1125971',
                    color: '#FFC46C'
                },
                {
                    line: true
                },
                {
                    title: '科幻是未来的钥匙——科幻启示录【科幻题材】',
                    href: 'https://m.douban.com/doulist/4253902',
                    color: '#2384E8'
                },
                {
                    title: '美国生活面面观',
                    href: 'https://m.douban.com/doulist/121326',
                    color: '#3BA94D'
                },
                {
                    title: '2015终极期待',
                    href: 'https://m.douban.com/doulist/37479562',
                    color: '#42BD56'
                },
                {
                    title: '经典韩国电影——收集100部',
                    href: 'https://m.douban.com/doulist/458087',
                    color: '#CC3344'
                }
            ]
        }
    },
    mutations: {
        getMovie(state, payload) {
            switch (payload.tag) {
                case 'hotMovies':
                    state.hotMovies = payload.result;
                    break;
                case 'newMovies':
                    state.newMovies = payload.result;
                    break;
                case 'topMovies':
                    state.topMovies = payload.result;
                    break;
                default:
                    break;
            }
        }
    },
    actions: {
        // getMovie({ commit, state }) {
        //     axios.get('/v2/movie/in_theaters?count=8')
        //         .then(function (result) {
        //             commit({
        //                 type: 'getMovie',
        //                 tag: 'hotMovies',
        //                 result: result.data
        //             })
        //         })
        //         .catch(function (error) {
        //             console.log(error);
        //         })

        //     axios.get('/v2/movie/coming_soon?count=8')
        //         .then(function (result) {
        //             commit({
        //                 type: 'getMovie',
        //                 tag: 'newMovies',
        //                 result: result.data
        //             })
        //         })
        //         .catch(function (error) {
        //             console.log(error);
        //         })

        //     axios.get('/v2/movie/top250?count=8')
        //         .then(function (result) {
        //             commit('getMovie', { tag: 'topMovies', result: result.data })
        //         })
        //         .catch(function (error) {
        //             console.log(error);
        //         })
        // }
        getMovie ({commit, state}, payload) {
            const hotMoviesReq = axios.get('/v2/movie/in_theaters?count=8');
            const newMoviesReq = axios.get('/v2/movie/coming_soon?count=8');
            const topMovies = axios.get('/v2/movie/top250?count=8');

            return axios.all([hotMoviesReq, newMoviesReq, topMovies]).then(function(result){
                result.forEach(function(instace,idx){
                    switch (idx) {
                        case 0:
                            commit('getMovie', {tag: 'hotMovies', result: instace.data });
                            break;
                        case 1: 
                            commit('getMovie', {tag: 'newMovies', result: instace.data});
                            break;
                        case 2: 
                            commit('getMovie', {tag: 'topMovies', result: instace.data});
                            break;
                        default:
                            break;
                    }
                });
            }).catch(err => {
                console.log(err)
            })
        }
    }
}
export default movie;